package com.huaxin.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * drawableTop与文本一起居中显示
 * Created by Administrator on 2016/9/27.
 */

public class DrawableTopCenterTextView extends TextView {

    public DrawableTopCenterTextView(Context context, AttributeSet attrs,
                                     int defStyle) {
        super(context, attrs, defStyle);
    }

    public DrawableTopCenterTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawableTopCenterTextView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
            Drawable drawableLeft = drawables[1];
            if (drawableLeft != null) {
                float textHeight = getLineCount()*getLineHeight();
                int drawablePadding = getCompoundDrawablePadding();
                int drawableHeight = drawableLeft.getIntrinsicHeight();
                float bodyheight = textHeight + drawableHeight + drawablePadding;
                canvas.translate(0, (getHeight() - bodyheight - getPaddingTop() - getPaddingBottom()) / 2 );

            }
      
        }
        super.onDraw(canvas);
    }
}
